import json
from datetime import datetime

from django.forms import model_to_dict
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from zuoye20250922.models import Garment, Category


# Create your views here.
@csrf_exempt
def post_category(request):
    data=json.loads(request.body)
    if data['name']=='' or data['details']=='':
        return JsonResponse(
            {
                'msg':'参数必填'
            }
        )
    a=Category.objects.create(
        name=data['name'],
        details=data['details'],
        day=datetime.now()
    )
    return JsonResponse(
        {
            'msg':'添加成功',
            'item':model_to_dict(a)
        }
    )
@csrf_exempt
def add_garment(request):
    data=json.loads(request.body)
    if data['name']=='' or data['price']=='':
        return JsonResponse(
            {
                'msg':'参数必填'
            }
        )
    if Category.objects.filter(category_id=data['category_id']).exists() is False:
        return JsonResponse(
            {
                'msg': '没有该类目'
            }
        )
    if data['price']<=0:
        return JsonResponse(
            {
                'msg':'价格需为正数'
            }
        )
    a=Garment.objects.create(
        name=data['name'],
        price=data['price'],
        category_id_id=data['category_id']
    )
    return JsonResponse(
        {
            'msg':'添加成功',
            'item':model_to_dict(a)
        }
    )
@csrf_exempt
def del_category(request):
    data=json.loads(request.body)
    if data['category_id_id']=='':
        return JsonResponse(
            {
                'msg':'参数必填'
            }
        )
    if Garment.objects.filter(category_id_id=data['category_id_id']).exists() is True:
        m=Garment.objects.filter(category_id_id=data['category_id_id'])
        l=[]
        for i in m:
            l.append(
                {
                    'id':i.id,
                    'name':i.name,
                    'price':i.price,
                    'category_id':i.category_id_id
                }
            )
        return JsonResponse(
            {
                'msg':'该类目下有商品',
                'items':l
            }
        )
    del_c=Category.objects.filter(category_id=data['category_id_id'])
    d=del_c.delete()
    return JsonResponse(
        {
            'msg':'删除成功'
        }
    )
@csrf_exempt
def del_garment(request):
    data=json.loads(request.body)
    if data['id']=='':
        return JsonResponse(
            {
                'msg':'参数必填'
            }
        )
    if Garment.objects.filter(id=data['id']).exists() is False:
        return JsonResponse(
            {
                'msg':'id错误'
            }
        )
    del_c=Garment.objects.filter(id=data['id'])
    d=del_c.delete()
    return JsonResponse(
        {
            'msg':'删除成功'
        }
    )
@csrf_exempt
def update_category(request):
    data=json.loads(request.body)
    if data['id']=='' or data['category_id_id']=='':
        return JsonResponse(
            {
                'msg':'参数必填'
            }
        )
    g=Garment.objects.filter
    gr=Garment.objects.get(id=data['id'])
    if g(id=data['id']).exists() is False:
        return JsonResponse(
            {
                'msg':'id错误'
            }
        )
    if gr.category_id_id==data['category_id_id']:
        return JsonResponse(
            {
                'msg':'修改类目不可为当前类目'
            }
        )
    u=Garment.objects.get(id=data['id'])
    u.category_id_id=data['category_id_id']
    u.save()
    return JsonResponse(
        {
            'msg':'修改成功',
            'item':model_to_dict(u)
        }
    )
@csrf_exempt
def get_garment(request):
    data=Garment.objects.all()
    u=json.loads(request.body)
    l=[]
    for i in data:
        # if i.category_id.name==u['category_name'] or u['category_name']=='':
        #         l.append(
        #             {
        #                 'name': i.name,
        #                 'price': i.price,
        #                 'category_id': i.category_id_id,
        #                 'category':{
        #                     'id':i.category_id.category_id,
        #                     'category_name':i.category_id.name,
        #                 }
        #             }
        #         )
        if i.category_id.category_id==u['id'] or u['id']=='':
                l.append(
                    {
                        'name': i.name,
                        'price': i.price,
                        'category_id': i.category_id_id,
                        'category':{
                            'id':i.category_id.category_id,
                            'category_name':i.category_id.name,
                        }
                    }
                )
        # if i.price>=u['price_min'] and i.price<u['price_max'] :
        #     l.append(
        #         {
        #             'name': i.name,
        #             'price': i.price,
        #             'category_id': i.category_id_id,
        #             'category': {
        #                 'id': i.category_id.category_id,
        #                 'category_name': i.category_id.name,
        #             }
        #         }
        #     )

    return JsonResponse(
        {
            'msg':'查询成功',
            'item':l
        }
    )